/**
 *    Copyright 2009-2021 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.type;

import org.apache.ibatis.executor.result.ResultMapException;
import org.apache.ibatis.session.Configuration;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 基类{@link TypeHandler}用于引用泛型类型。
 * <p>
 * 重要提示:从3.5.0开始，该类从不调用{@link ResultSet#wasNull()}和{@link CallableStatement#wasNull()}方法来处理SQL {@code NULL}值。
 * 换句话说，应该在子类上执行{@code null}值处理。
 * </p>
 *
 * @author Clinton Begin
 * @author Simone Tripodi
 * @author Kzuki Shimizu
 */
public abstract class BaseTypeHandler<T> extends TypeReference<T> implements TypeHandler<T> {

    /**
     * @deprecated Since 3.5.0 - See https://github.com/mybatis/mybatis-3/issues/1203. 这个字段将移除未来。
     */
    @Deprecated
    protected Configuration configuration;

    /**
     * 设置配置。
     *
     * @param c 新的配置
     * @deprecated Since 3.5.0 - See https://github.com/mybatis/mybatis-3/issues/1203. 此属性将删除future。
     */
    @Deprecated
    public void setConfiguration(Configuration c) {
        this.configuration = c;
    }

    @Override
    public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
        if (parameter == null) {
            //当参数值为空时， 设值
            if (jdbcType == null) {
                throw new TypeException("JDBC 要求必须为所有可为空的参数指定JdbcType。");
            }

            try {
                ps.setNull(i, jdbcType.TYPE_CODE);
            } catch (SQLException e) {
                throw new TypeException("错误: 以JdbcType[" + jdbcType + "]设置参数 #" + i + "出错。 尝试为这个参数设置不同的JdbcType或不同的jdbcTypeForNull配置属性。Cause: " + e, e);
            }
        } else {
            try {
                setNonNullParameter(ps, i, parameter, jdbcType);
            } catch (Exception e) {
                throw new TypeException("错误: 以JdbcType[" + jdbcType + "]设置参数 #" + i + "出错。尝试为该参数或不同的配置属性设置不同的JdbcType. Cause: " + e, e);
            }
        }
    }

    @Override
    public T getResult(ResultSet rs, String columnName) throws SQLException {
        try {
            return getNullableResult(rs, columnName);
        } catch (Exception e) {
            throw new ResultMapException("错误: 试图从结果集中获取列 '" + columnName + "' 出错.  Cause: " + e, e);
        }
    }

    @Override
    public T getResult(ResultSet rs, int columnIndex) throws SQLException {
        try {
            return getNullableResult(rs, columnIndex);
        } catch (Exception e) {
            throw new ResultMapException("错误: 试图从结果集中获取列 #" + columnIndex + " 出错.  Cause: " + e, e);

        }
    }

    @Override
    public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
        try {
            return getNullableResult(cs, columnIndex);
        } catch (Exception e) {
            throw new ResultMapException("错误: 试图从回调的Statement中获取列 #" + columnIndex + " 出错.  Cause: " + e, e);

        }
    }

    /**
     * 为sql的参数设值，
     *
     * @param ps        PreparedStatement对象
     * @param i         SQL 参数位置
     * @param parameter 不为空的参数值
     * @param jdbcType  JdbcType类型
     * @throws SQLException QL异常
     */
    public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;

    /**
     * 获取可为空的结果。
     *
     * @param rs         the rs
     * @param columnName 列名, when configuration <code>useColumnLabel</code> is <code>false</code>
     * @return 可空的结果
     * @throws SQLException the SQL exception
     */
    public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException;

    public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException;

    public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException;
}
